<?php 
$req_path="./";
include_once($req_path."include/ini.php");
require_once($req_path."include/db.php");

class M_category extends DB{
	private $dbConn;
	private $rowCount="0";
	private $ID="0";
	private $sqlSelect ="select CatID, CatName , case when status=1 then 'Actived' else 'Disabled' end Status from tb_product_category where IsDeleted=0 ";
	private $totalpage="0";
	function M_category(){
		parent::__construct();
	}
	function getText(){
		echo "TEST";
	}
	function getListPage(){
		global $list_page;
		return $list_page;
	}
	function getData( $name=null ,$pageStart=1 ){
		global $list_page;
		$sqlscript= $this->sqlSelect;
		
		if (isset($name) && $name!=""){
			$sqlscript.= " and CatName like '%". $name."%'";
		}
		
		//Order 
		$sqlscript.= " order by CatName asc " ;
		//Execute
		$result = $this->ExecuteData($sqlscript);
		//Get all rows
		$rows =$result->RecordCount();
	
		//Page limit
		$sqlscript.= " limit ". ($pageStart-1)*$list_page. ", " . $list_page;
		//paging
		$result = $this->ExecuteData($sqlscript);		
	
		$totalpage=1;
		if ($rows>0){
			$rt = $rows%$list_page;
			
			if($rt!=0) {
				$totalpage = floor($rows/$list_page)+1;
			}
			else {
				$totalpage = floor($rows/$list_page);
			}
		}
		
		$this->rowCount=$rows;
		$this->totalpage=$totalpage;	
		
		return $result;
	}
	
	function  checkCategory($name){
		$sqlscript= "select * ";
		$sqlscript.= " from tb_product_category where IsDeleted=0 ";
		$sqlscript.= " and CatName='". $name."'";
		$result = $this->ExecuteData($sqlscript);	
		
		$rows=$result->RecordCount();
		$val=$rows > 0? "0":"1";
		return $val;
	}
	
	function getDataDetail($id=0){
		$sqlscript= "select CatID, CatName , Status ";
		$sqlscript.= " from tb_product_category where IsDeleted=0 ";
		$sqlscript.= " and CatID='". $id."'";
		$result = $this->ExecuteData($sqlscript);
		return $result->FetchRow();
	}

	function getRowCount(){
		return $this->rowCount;
	}
	
	function getLastInsert(){
		return $this->ID;
	}
	
	function InsertData($val){
		if (isset($val)){
			$sqlInsert=$this->InsertSqlScript($this->sqlSelect, $val);
			$this->ExecuteData($sqlInsert);
			$this->ID = $this->getLastID();
			return $this->ID;
		}
	}
	function updateData($sql, $val){
		if(isset($val)){
			$sqlUpdate=$this->UpdateSqlScript($sql,$val);
			return $this->ExecuteData($sqlUpdate);
		}
	}
	function delData($id){
		if(isset($id)){
			$sqlDelete = "update tb_product_category set IsDeleted=1 where CatID='".$id."'";
			$this->ExecuteData($sqlDelete);
			return "c";
		}
	}
		function getTotalPage(){
		return $this->totalpage;
	}
}
?>